<template>
  <CopyOrMoveDialog
    v-if="isDialogVisible"
    :files="files"
    :visible.sync="isDialogVisible"
    :action="action"
    :api="api"
    @success="success" />
</template>

<script>
  import CopyOrMoveDialog from '@/components/x-file-explorer-lite/plugins/CopyOrMoveDialog'

  export default {
    name: 'CopyMovePlugin',
    components: { CopyOrMoveDialog },
    inject: ['explorer'],
    props: {
      api: Function
    },
    data() {
      return {
        action: '',
        isDialogVisible: false,
        files: []
      }
    },
    methods: {
      move(file) {
        this.executeAction('move', file)
      },
      copy(file) {
        this.executeAction('copy', file)
      },
      executeAction(action, file) {
        const { explorer } = this
        if (file) {
          this.files = [file]
        } else {
          this.files = explorer.selectedRows?.length ? explorer.selectedRows : [explorer.currentRow]
        }
        this.action = action
        this.isDialogVisible = true
      },
      success() {
        const { explorer } = this
        this.isDialogVisible = false
        if (this.action === 'move') {
          explorer.fetchFile()
        }
        this.$message.success(`${this.action === 'move' ? '移动' : '复制'}成功!`)
      }
    }
  }
</script>
